﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormExam
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        public static Dictionary<string, string> users = new Dictionary<string, string>();

       

        /// <summary>
        /// 登录页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            //获取用户输入信息
            string uName = this.txtUserName.Text.Trim();
            string uPwd = this.txtUserPwd.Text.Trim();

            //判断是否为空
            if (string.IsNullOrEmpty(uName))
            {
                MessageBox.Show("用户名不能为空!", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUserName.Focus();
                return;
            }
            if (string.IsNullOrEmpty(uPwd))
            {
                MessageBox.Show("密码不能为空!", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUserPwd.Focus();
                return;
            }

            //string conString = "server = .; uid = sa; pwd = 123456; database = Blogs";
            //var usr = users.Where(x => x.Key == uPwd && x.Value == uName)
            //        .Select(x => new { key = x.Key, value = x.Value }).FirstOrDefault();

            var sql = string.Format("select * from Users where UserName='{0}' and PassWord='{1}'", uName, uPwd);
            var dt = DbHelper.GetDataTable(sql);

            if (dt.Rows.Count >= 1)
            {
                MessageBox.Show("登录成功", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                MainForm mainForm = new MainForm();
                this.Hide();
                mainForm.ShowDialog();
            }

            else
            {
                MessageBox.Show("用户名或密码错误", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //users.Add("小Q", "123");
            
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            RegisterForm registerForm = new RegisterForm();
            registerForm.Show();

       
        }
    }
}
